<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/12
     * Time: 下午8:54
     */

    namespace app\store\admin;

    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\member\model\MemberInfoModel;
    use app\store\model\AddressModel;
    use app\store\model\CategoryModel;
    use think\Db;

    class Address extends Admin
    {


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function lists()
        {
            // 查询
            $map = $this->getMap();
            // 排序
            $order = $this->getOrder('id desc');

            $data_list = AddressModel::field('*')->where($map)->order($order)->paginate();

            return ZBuilder::make('table')->setPageTitle('地址管理')->setSearch(['title' => '姓名名称'])// 设置搜索框
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['member_id', '用户名称', 'text'],
                ['name', '收货人姓名', '', MemberInfoModel::where([])->column('member_id as id,nickName')],
                ['mobile', '联系人', 'text'],
                ['address_area', '行政区', 'callback', function ($value) {
                    return join('-',AddressModel::getOrigin($value));
                }],
                ['address_detail', '详细地址', 'text'],
                ['is_default', '是否上架', 'yesno'],
                ['is_default', '是否上架', 'switch'],
                ['right_button', '操作', 'btn']
            ])->addTopButtons(['enable', 'disable', 'delete'])// 批量添加顶部按钮
            ->addRightButtons(['edit', 'delete' => ['data-tips' => '删除后无法恢复。']])// 批量添加右侧按钮
            ->addOrder('id')->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染模板
        }

        /**
         * 编辑
         *
         * @param null $id
         *
         * @return mixed
         * @throws Exception
         * @throws \think\exception\DbException
         */
        public function edit($id = null)
        {

            if ($id === null)
                $this->error('缺少参数');

            // 保存数据
            if ($this->request->isPost()) {
                // 表单数据
                $data = $this->request->post();
                $data['id'] = $data['id'];
                if (isset($data['feature'])) {
                    $data['feature'] = join(',', $data['feature']);
                }

                unset($data['id']);

                if (AddressModel::update($data, ['id' => $data['id']])) {
                    //dump(Db::name('')->getLastSql());die;
                    // 记录行为
                    action_log('AddressModel_edit', null, $id, session('admin_id'), $id);

                    $this->success('编辑成功', 'lists');
                } else {
                    $this->error('编辑失败');
                }
            }

            $info = AddressModel::field('*,id as id')->where(['id' => $id])->find();

            // 显示编辑页面
            return ZBuilder::make('form')->addFormItems([
                ['hidden', 'id'],
                ['text', 'goods_name', '商品名称'],
                ['select', 'storeclass_id', '分类名称', '', CategoryModel::where([])->column('id,gc_name')],
                ['select', 'member_id', '商家名称', '', StoreUserModel::getSelect()],
                ['image', 'goods_image', '广告图'],
                ['images', 'goods_images', '轮播图'],
                ['text', 'goods_price', '商品价格'],
                ['text', 'goods_store_price', '商品优惠价格'],
                ['text', 'royalty', '分红', '分红：例如：1.5  1.5元'],
                ['ueditor', 'goods_body', '商品详情描述'],
                ['text', 'goods_num', '库存'],
                ['text', 'store_pphone', '联系电话'],
                ['checkbox', 'feature', '栏目选择', '', StoreFeatureModel::getColumn('action,title')],
                ['radio', 'goods_commend', '是否推荐', '', ['否', '是'], 1],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])->setFormData($info)->fetch();
        }

        /**
         * 删除
         *
         * @param array $record
         *
         * @return mixed
         */
        public function delete($record = [])
        {
            return $this->setStatus('delete', new AddressModel);
        }

        /**
         * 启用
         *
         * @param array $record
         *
         * @return mixed
         */
        public function enable($record = [])
        {
            return $this->setStatus('enable', new AddressModel);
        }

        /**
         * 禁用
         *
         * @param array $record
         *
         * @return mixed
         */
        public function disable($record = [])
        {
            return $this->setStatus('disable', new AddressModel);
        }

        /**
         * 设置状态：删除、禁用、启用
         *
         * @param string $type
         * @param array $record
         * @param null $Model
         *
         * @return mixed
         */
        public function setStatus($type = '', $record = [], $Model = null)
        {
            $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
            $link_title = AddressModel::where('id', 'in', $ids)->column('goods_name');
            if ($Model) {
                return parent::setStatus($type, [
                    'AddressModel_' . $type,
                    null,
                    0,
                    session('admin_id'),
                    implode('、', $link_title)
                ], $Model);
            } else {
                return parent::setStatus($type, [
                    'AddressModel_' . $type,
                    null,
                    0,
                    session('admin_id'),
                    implode('、', $link_title)
                ], new AddressModel);

            }
        }

        /**
         * 快速编辑
         *
         * @param array $record
         * @param null $Model
         */
        public function quickEdit($record = [], $Model = null)
        {
            $id = input('post.pk', '');
            $field = input('post.name', '');
            $value = input('post.value', '');
            $AddressModel = AddressModel::where('id', $id)->value($field);
            $details = '字段(' . $field . ')，原值(' . $AddressModel . ')，新值：(' . $value . ')';
            return parent::quickEdit(['link_edit', 'cms_link', $id, session('admin_id'), $details], new AddressModel());
        }
    }